# -*- coding:utf-8 -*-
try:
    import maya.cmds as cmds
except Exception as test_ERR:
    print('test')



def camera_check():

    sobj = cmds.ls(sl=1,l=1)
    obj_transform = sobj[0] if sobj else None
    shape = cmds.listRelatives(sobj)[0]
    a = cmds.listAttr(shape, st='lensSqueezeRatio')
    if a:
        return True,obj_transform,shape
    else:
        return False,obj_transform,shape


def lock_cam(camShape,lock):
    attr_list = ['tx','ty','tz','rx','ry','rz','sx','sy','sz','hfa','vfa','fl','lsr','fs','fd','sa','coi','lls']
    if lock:
        for a in attr_list:
            cmds.setAttr(camShape+'.'+a,lock=1)
    else:
        for a in attr_list:
            cmds.setAttr(camShape+'.'+a,lock=0)


def set_main_camera(shot_name):
    iscam,obj_transform,shape = camera_check()
    if iscam:
        # shot_name = cmds.getAttr('persp.asset_shot')
        cameras = cmds.ls(type='camera', l=1)
        # print(cameras)
        #transforms = cmds.listRelatives(cameras, allParents=True)
        # print(rr)
        for i in cameras:
            split_shape = i.split('|')
            transform = split_shape[-2]
            new_slpit = split_shape[:-1]
            long_name = '|'.join(new_slpit)  #get long name
            if transform == shot_name:
                cmds.rename(long_name,'camera')


        cmds.rename(obj_transform,shot_name)
        # cmds.rename(shape,shot_name+'Shape')
    return iscam


def _run():
    try:
        set_main_camera()
    except Exception as set_main_camera_ERR:
        print('set_main_camera_ERR:',str(set_main_camera_ERR))




def _doc():
    doc = {'name':u'设置主相机','doc':'将当前选择的相机设置为主相机'}
    return doc